﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESF.General.MVP;
using ESF.General;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.Entities;
using ESF.General.Entity;
using ESF.SSO.Core.BusinessLogic;

#endregion 


namespace ESF.SSO.Presenter.Client
{
	public class SignUpPresenter : BasePresenter<ISignUpViewer>
	{
		#region Constructor
		
		public SignUpPresenter (ISignUpViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones Estaticas
		
		public static BrowseResults<BasicReference> QueryCountriesByName (string prefixText, int count)
		{
			return CtrlBasicReference.Browse (
				new BasicReference () { Name = prefixText, BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.Country }}, 
				new PaggingInfo () { PageIndex = 0, PageSize = count });
		}


		public static BrowseResults<BasicReference> QueryCitiesByName(string prefixText, int count)
		{
			return CtrlBasicReference.Browse(
				new BasicReference() { Name = prefixText, BasicReferenceType = new BasicReferenceType() { ID = (int)BasicReferenceTypes.City } },
				new PaggingInfo() { PageIndex = 0, PageSize = count });
		}
		
		#endregion
		
		
		#region Funciones de Presentacion
		
		public override void Load ()
		{
			LoadSteps ();
		
			Clear ();
		
			LoadLists ();
		}
		
		
		public void LoadSteps ()
		{
			IEnumerable<BasicReference> apps;
			
			
			apps = CtrlBasicReference.List (BasicReferenceTypes.Application);
		
			_Viewer.ExtraAppSteps = apps.Where (a => !string.IsNullOrEmpty (a.Extra_II)).ToList ();
		}
		
		
		public void LoadLists ()
		{
		}
		
		
		public override void Clear ()
		{
			ClearError ();
			
			_Viewer.UserAddress = string.Empty;
			_Viewer.UserAlias = string.Empty;
			_Viewer.UserBirthDate = null;
			_Viewer.UserCellPhone = string.Empty;
			_Viewer.UserEmail = string.Empty;
			_Viewer.UserEnterprise = string.Empty;
			_Viewer.UserFirstName = string.Empty;
			_Viewer.UserGenderID = null;
			_Viewer.UserHomePhone = string.Empty;
			_Viewer.UserIdentificationNumber = string.Empty;
			_Viewer.UserIdentificationTypeID = 0;
			_Viewer.UserLastName = string.Empty;
			_Viewer.UserPassword = string.Empty;
			_Viewer.UserSecondName = string.Empty;
			_Viewer.UserSecurityQuestionAnswer = string.Empty;
			_Viewer.UserSecurityQuestionID = 0;
			_Viewer.UserSurName = string.Empty;
			_Viewer.UserLocationCityName = string.Empty;
			_Viewer.UserProfession = string.Empty;
			_Viewer.UserPosition = string.Empty;
		}
		
		#endregion
		
		
		#region Funciones	
		
		public bool SignUp ()
		{
			User user;
		
		
			user = new User ()
			{
				Alias = _Viewer.UserAlias,
				BirthDate = _Viewer.UserBirthDate,
				Email = _Viewer.UserEmail,
				Enterprise = new Enterprise () { Name = _Viewer.UserEnterprise },
				FirstName = _Viewer.UserFirstName,
				Gender = new BasicReference () { ID = _Viewer.UserGenderID },
				IdentificationNumber = _Viewer.UserIdentificationNumber,
				IdentificationType = new BasicReference () { ID = _Viewer.UserIdentificationTypeID },
				LastName = _Viewer.UserLastName,
				Password = _Viewer.UserPassword,
				SecondName = _Viewer.UserSecondName,
				SecurityQuestion = new BasicReference () { ID = _Viewer.UserSecurityQuestionID },
				SecurityQuestionAnswer = _Viewer.UserSecurityQuestionAnswer,
				SurName = _Viewer.UserSurName,
				IsEnabled = false,
				Profession = _Viewer.UserProfession,
				Position = _Viewer.UserPosition,
				
				Locations = new List<Location> 
				{
					new Location ()
					{
						Address = _Viewer.UserAddress,
						CellPhoneNumber = _Viewer.UserCellPhone,
						Country = new BasicReference () { Name = _Viewer.UserLocationCountryName },
						City = new BasicReference () { Name = _Viewer.UserLocationCityName },
						Name = "Principal",
						PhoneNumber = _Viewer.UserHomePhone
					}
				}
			};
		
			try
			{
				_Viewer.UserID = CtrlUser.SignUp (user);
				
				_Viewer.frmInfoMsg = "Su cuenta de usuario ha sido solicitada.\n\nCuando este activada se le enviara un mensaje a su correo electrónico";
				
				return true;
			}
			catch (Exception pEx)
			{
				LogError (pEx);
				
				return false;
			}
		}
		
		#endregion
	}
}
